package com.source.module_kotlin_library.widget

import android.content.Context
import android.content.res.Resources
import android.util.AttributeSet
import android.util.TypedValue
import kotlin.math.min

class SquareImageView : android.support.v7.widget.AppCompatImageView {
    constructor(context: Context?) : this(context, null)
    constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var width = min(measuredWidth, measuredHeight)
        setMeasuredDimension(width, width)
    }
    override fun layout(l: Int, t: Int, r: Int, b: Int) {
        super.layout(l, t, (l + dp2px(100f)).toInt(), (t + dp2px(100f)).toInt())
    }


    fun dp2px(dpValue: Float): Float {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpValue, Resources.getSystem().displayMetrics)
    }
}